import { IEvent } from '../type/IEvent';
import { BNode } from '../type/BNode';

export class DelayNode extends BNode implements IEvent {
    iTime = 0;
    isPause = false;

    constructor(public maxTime: number) {
        super();
    }

    onStart(blackboard?: any): void {
        this.iTime = 0;
    }

    onUpdate(dt: number): void {
        if (this.isPause) {
            return;
        }
        this.iTime += dt;
        if (this.iTime >= this.maxTime) {
            this.task.done();
        }
    }

    onCancel(): void {
        this.iTime = 0;
    }

    onReset(): void {
        this.iTime = 0;
    }

    onEvent(event: string, data?: any) {
        switch (event) {
            case 'pause':
                this.isPause = true;
                break;
            case 'resume':
                this.isPause = false;
                break;
        }
    }
}
